package cn.itsource.ymcc.mq;

import cn.itsource.ymcc.constant.NoticeConstant;
import cn.itsource.ymcc.domain.MessageEmail;
import cn.itsource.ymcc.domain.MessageSms;
import cn.itsource.ymcc.dto.CourseNoticeDto;
import cn.itsource.ymcc.service.IMessageEmailService;
import cn.itsource.ymcc.service.IMessageSmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RocketMQMessageListener(topic = NoticeConstant.TOPIC,
        consumerGroup = "courseNotice-email",
        selectorExpression = "*",
        messageModel = MessageModel.BROADCASTING
)
@Slf4j
public class EmailComsumer implements RocketMQListener<CourseNoticeDto> {

    @Autowired
    private IMessageEmailService emailService;

    @Override
    public void onMessage(CourseNoticeDto courseNoticeDto) {
        MessageEmail email = new MessageEmail();
        email.setTitle("课程发布");
        email.setEmail("651517957@qq.com");
        String content = String.format(NoticeConstant.COURSE_ONLINE_CONET,courseNoticeDto.getPath(),courseNoticeDto.getTitle() );
        email.setContent(content);
        email.setSendTime(new Date());
        // 查询用户列表，获取用户id;
        email.setUserId(1L);

        // 利用Email工具类，发送邮件


        emailService.insert(email);
        log.info("发送邮件通知成功{}",email);
    }
}
